﻿using AutoMapper;
using B.S.Business.Domain;
using B.S.Business.ErrorCode;
using B.S.Business.Infacetrue;
using B.S.Business.Write.API.Applications.Command.SupplierCommand;
using MediatR;
using Yitter.IdGenerator;

namespace B.S.Business.Write.API.Applications.CommandHandler.SupperlierCommandHandler
{
    public class SupplierUpdCommandHandler : IRequestHandler<SupplierUpdCommand, ApiResult<object>>
    {
        private readonly IBaseRepository<SupplierApp> supplierRepo;
        private readonly IMapper mapper;

        public SupplierUpdCommandHandler(IBaseRepository<SupplierApp> supplierRepo, IMapper mapper)
        {
            this.supplierRepo = supplierRepo;
            this.mapper = mapper;
        }
        /// <summary>
        /// 供应商更新处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<object>> Handle(SupplierUpdCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> result = new ApiResult<object>();
            var m = mapper.Map<SupplierApp>(request);

            m.SupplierNumber = YitIdHelper.NextId().ToString();
            m.SupplierName = request.SupplierName;
            m.OrderAmount = request.OrderAmount;
            m.SupplierPhone = request.SupplierPhone;
            m.ChuanZhen = request.ChuanZhen;
            m.SupplierName = request.SupplierName;
            m.Email = request.Email;
            m.SupplierNamer = request.SupplierNamer;
            m.SupplierAdd = request.SupplierAdd;
            m.SupplierTime = request.SupplierTime;

            var a = supplierRepo.Update(m);
            if (a > 0)
            {
                result.Code = ApiCode.修改成功;
                result.Msg = "修改成功";
                result.Data = a;

            }
            else
            {
                result.Code = ApiCode.修改成功;
                result.Msg = "修改成功";
                result.Data = a;
            }
            return Task.FromResult(result);
        }
    }

}
